//
//  ChangeBirthdayVC.m
//  Cunli
//
//  Created by lilubin on 15/6/30.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ChangeBirthdayVC.h"
#import "MyDataService.h"
#import "Config.h"
#import "NSString+MD5.h"
#import "User.h"

@interface ChangeBirthdayVC ()
@property (weak, nonatomic) IBOutlet UITextField *birthdayField;
@property (nonatomic, strong) UIDatePicker *datePicker;


@end

@implementation ChangeBirthdayVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    if ([CLUser shareCLUser].birthday != nil) {
    if([Login curLoginDetailUser].birthday != nil ){
//        _birthdayField.text = [CLUser shareCLUser].birthday;
        _birthdayField.text = [Login curLoginDetailUser].birthday;
    }

    [self setMyDatePicker];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)setMyDatePicker
{
    self.navigationItem.title = @"出生日期";
    _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    
    //    NSDate *minDate = [[NSDate alloc] initWithString:@""];
    //    NSDate *minDate = [NSDate dateWithTimeIntervalSince1970:0];
    NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:10];
    
    //    _datePicker.minimumDate = minDate;
    _datePicker.maximumDate = maxDate;
    
    [_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
//    [self.view addSubview:_datePicker];
    
    _birthdayField.inputView = _datePicker;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
//    [rightBtn setBackgroundImage:[UIImage imageNamed:@"regnext"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];    
}

- (void)dateChange:(UIDatePicker *)datePicker
{
//    NSString *timeString = [NSString stringWithFormat:@"%@", _datePicker.date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [dateFormatter stringFromDate:_datePicker.date];
//    _birthdayField.text = [timeString substringToIndex:10];
    _birthdayField.text = dateStr;
}

- (void)commitAction:(UIButton *)button
{
    [self.view endEditing:YES];
    
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:4];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:_birthdayField.text forKey:@"value"];
    [CheckParams setObject:@"birthday" forKey:@"field"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"edit_profile.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
        if ([result[@"response"] isEqualToString:@"edit_profile"]) {
//            [CLUser shareCLUser].birthday = _birthdayField.text;
            [Login curLoginDetailUser].birthday = _birthdayField.text;
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failureAction:^{
        NSLog(@"生日修改失败");
        [Config showHUDWithText:@"网络错误" andImage:@"error"];
    }];
    
}

@end
